package pnu.ssm.controller.manager;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import pnu.ssm.model.job.Job;

// Singleton Pattern
// Iterator Pattern
public class JobManager implements Iterable<Job> {
	private Queue<Job> jobs;
	
	private static JobManager instance = new JobManager();
	private JobManager() {
		jobs = new LinkedList<Job>();
	}
	public static JobManager getInstance(){
		return instance;
	}
	
	public void init(){
		jobs.clear();
	}
	
	public void addJob(Job j){
		jobs.add(j);
	}
	
	public Job removeJob(){
		Iterator<Job> it = iterator();
		while(it.hasNext()){
			Job tempJob = it.next();
			
			if(!tempJob.getBlock().isMoving()){
				jobs.remove(tempJob);
				return tempJob;
			}
		}
		return null;
	}
	
	public int getSize(){		
		return jobs.size();
	}

	@Override
	public Iterator<Job> iterator() {
		return jobs.iterator();
	}
	
	public boolean isEmpty(){
		return getSize()==0;
	}
}
